package org.osite.xml;

import org.osite.util.Nullable;
import org.w3c.dom.Node;

public class XmlParseException extends Exception {

  private static final long serialVersionUID = 4977495863706045253L;
  private Node where;

  public XmlParseException(String message, @Nullable Node where) {
    super(message);
    this.where = where;
  }

  public XmlParseException(String message) {
    super(message);
  }

  public XmlParseException(Throwable cause) {
    super(cause);
  }

  public XmlParseException(String message, Throwable cause) {
    this(message, cause, null);
  }

  public XmlParseException(
      String message, Throwable cause, @Nullable Node where) {
    super(message, cause);
    this.where = where;
  }

  public void setWhere(Node node) {
    this.where = node;
  }

  @Override
  public String getMessage() {
    String path = "unknown path";
    try {
      if (where != null) {
        path = XmlUtil.getNodePath(where);
      }
    } catch (RuntimeException e) {
      // ignore the exception with unknown path
    }
    return (super.getMessage() + " @" + path);
  }
}
